package cn.gemframe.sms;

import cn.gemframe.utils.GemMD5Utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class MessageUtils {
	
	@SuppressWarnings("unused")
	public static synchronized boolean send(String mobile,String content){
        BufferedReader in = null;
        String result = "";
        PrintWriter printWriter = null;  
		try {
		    String server = "http://sdk.entinfo.cn:8061/webservice.asmx/mdsmssend?";
		    String sn="SDK-BBX-010-23932";
		    String pwd="";
		    String md5pwd= GemMD5Utils.mmd5(sn+pwd);
		    //短信内容后缀必须加上【签名】
		    String codeContent=URLEncoder.encode("【自定义签名】"+content,"UTF-8");
	        String params="sn="+sn+"&pwd="+md5pwd+"&mobile="+mobile+"&content="+codeContent+"&ext=&stime=&rrid=&msgfmt=";
	        URL url = new URL(server);
	        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setRequestMethod("POST");
		    // 发送POST请求必须设置如下两行  
			conn.setDoOutput(true);  
			conn.setDoInput(true);  
            // 获取URLConnection对象对应的输出流  
            printWriter = new PrintWriter(conn.getOutputStream());  
            // 发送请求参数  
            printWriter.write(params);  
            // flush输出流的缓冲  
            printWriter.flush();  
			conn.connect(); 
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"gb2312"));
			String line;
			while ((line = in.readLine())!= null){
				 result = line;
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	public static void main(String[] args) {
		MessageUtils.send("15116916944","欢迎注册");
	}
}
